package com.ikunkun.controller.admin;


import com.ikunkun.constant.JwtClaimsConstant;
import com.ikunkun.constant.PasswordConstant;
import com.ikunkun.constant.StatusConstant;
import com.ikunkun.context.BaseContext;
import com.ikunkun.dto.EmployeeDTO;
import com.ikunkun.dto.EmployeeLoginDTO;
import com.ikunkun.dto.EmployeePageQueryDTO;
import com.ikunkun.entity.Employee;
import com.ikunkun.enumeration.OperationType;
import com.ikunkun.properties.JwtProperties;
import com.ikunkun.result.PageResult;
import com.ikunkun.service.EmployeeService;
import com.ikunkun.utils.JwtUtil;
import com.ikunkun.vo.EmployeeLoginVO;
import com.ikunkun.result.Result;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.DigestUtils;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Log4j2
@RestController
@Slf4j
@RequestMapping("/admin/employee")
public class EmployeeController {
    @Resource
    private EmployeeService employeeService;
    @Resource
    private JwtProperties jwtProperties;

    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        /*
         * @description: TODO
         * 
         * @param employeeLoginDTO
         * 
         * @return com.ikunkun.result.Result<com.ikunkun.vo.EmployeeLoginVO>
         * 
         * @author: L1kugg
         * 
         * @date: 2025/3/2 14:04
         */
        log.info("员工登录：{}", employeeLoginDTO);
        Employee employee = employeeService.login(employeeLoginDTO);
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);
        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();
        return Result.success(employeeLoginVO);
    }

    @PostMapping("/logout")
    @ApiOperation("员工退出")

    public Result<String> logout() {
        /*
         * @description: TODO
         * 
         * @param
         * 
         * @return com.ikunkun.result.Result
         * 
         * @author: L1kugg
         * 
         * @date: 2025/3/2 14:05
         */
        return Result.success();
    }

    @ApiOperation("新增员工")
    @PostMapping
    public Result save(@RequestBody EmployeeDTO employeeDTO) {
        log.info("新增员工：{}", employeeDTO);
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO, employee);
        employee.setStatus(StatusConstant.ENABLE);
        // 默认密码为123456
        employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));
        // 创建人、创建时间、修改人、修改时间
//         employee.setCreateTime(LocalDateTime.now());
//         employee.setUpdateTime(LocalDateTime.now());
//         employee.setCreateUser(BaseContext.getCurrentId());
//         employee.setUpdateUser(BaseContext.getCurrentId());
        employeeService.save(employee);
        return Result.success();
    }

    @ApiOperation("分页查询员工")
    @GetMapping("/page")
    public Result<PageResult> page(EmployeePageQueryDTO employeePageQueryDTO) {
        log.info("分页查询员工：{}", employeePageQueryDTO);
        return employeeService.pageQuery(employeePageQueryDTO);
    }


    @ApiOperation("编辑员工")
    @PutMapping
    public Result update(@RequestBody EmployeeDTO employeeDTO) {
        log.info("编辑员工：{}", employeeDTO);
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO, employee);
//         employee.setUpdateTime(LocalDateTime.now());
//         employee.setUpdateUser(BaseContext.getCurrentId());
         employeeService.updateById(employee);
        return Result.success();
    }
    @ApiOperation("根据id查询员工")
    @GetMapping("/{id}")
    public Result<Employee> getById(@PathVariable Long id) {
        log.info("根据id查询员工：{}", id);
        Employee employee = employeeService.getById(id);
        return Result.success(employee);
    }
    @PostMapping("/status/{status}")
    @ApiOperation("启用禁用员工")
    public Result startOrStop(@PathVariable Integer status, Long id) {
        log.info("启用禁用员工：{}, {}", status, id);
        employeeService.startOrStop(status, id);
        return Result.success();
    }




}
